<template>
  <div class="consumer-manange w-full h-full">
    <div class="btns w-full">
      <a-button type="primary" @click="consumerGroupDelete" :disabled="table.rowSelection.selectedRowKeys.length<1">删除</a-button>
    </div>
    <a-table
      style="height:calc(100% - 40px);overflow-y:auto;"
      bordered
      :rowKey="record => record.id"
      :columns="table.columns"
      :row-selection="table.rowSelection"
      :data-source="table.dataSource"
      :pagination="table.pagination"
    >
      <template slot="operation" slot-scope="text,record">
        <a-button @click="editDetail(record)" type="default">编辑</a-button>
      </template>
    </a-table>
  </div>
</template>

<script>
import orupService from '@/api/orupService'
import moment from 'moment'

const columns = [
  {
    title: '订阅组',
    align: 'center',
    dataIndex: 'groupName'
  },
  {
    title: 'ID',
    align: 'center',
    dataIndex: 'id'
  },
  {
    title: '数量',
    align: 'center',
    dataIndex: 'retryQueueNums'
  },
  {
    title: '创建时间',
    align: 'center',
    dataIndex: 'createTime',
    customRender: function (text, row, index) {
      return moment(text).format('YYYY-MM-DD HH:mm:ss')
    }
  },
  {
    title: '操作',
    align: 'center',
    scopedSlots: { customRender: 'operation' }
  }
]

export default {
  name: 'ConsumerManage',

  data () {
    return {
      filter: {
        filterText: '',
        pageIndex: 1,
        pageSize: 10
      },
      table: {
        columns: columns,
        dataSource: [],
        rowSelection: {
          selectedRowKeys: [],
          selectedRow: {},
          onChange: (selectedRowKeys, selectedRows) => {
            this.table.rowSelection.selectedRowKeys = selectedRowKeys.length ? [selectedRowKeys.pop()] : []
            this.table.rowSelection.selectedRow = selectedRows.length ? selectedRows.pop() : {}
          }
        },
        pagination: {
          pageSize: 10,
          total: 0,
          current: 1,
          showSizeChanger: true,
          pageSizeOptions: ['10', '20', '50', '100', '200', '500'],
          showTotal: (total) => `共${total}条数据`,
          onShowSizeChange: (current, pageSize) => {
            this.filter.pageIndex = 1
            this.filter.pageSize = pageSize
          },
          onChange: (current, pageSize) => {
            this.filter.pageIndex = current
            this.filter.pageSize = pageSize
            this.table.pagination.current = current
          }
        }
      }
    }
  },

  mounted () {
    this.loadGroupData()
  },

  methods: {
    moment,
    loadGroupData () {
      const params = {
        pageIndex: this.filter.pageIndex - 1,
        pageSize: this.filter.pageSize,
        filterText: this.filter.filterText,
        skipSysGroup: true
      }
      orupService.consumerGroupListQuery(params).then(res => {
        if (res && res.length > 0) {
          this.table.dataSource = res
        }
      })
    },
    consumerGroupDelete () {
      const id = this.table.rowSelection.selectedRow.id
      this.$confirm({
        title: '提示',
        content: '确定删除？',
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          orupService.consumerGroupDelete(id).then(res => {
            this.$message.success('删除成功!')
            this.table.rowSelection.selectedRowKeys = []
            this.table.rowSelection.selectedRow = {}
          })
        }
      })
    },
    editDetail (record) {
      this.$dialog({
        title: '详情',
        content: () => import('@v/investmentOnline/messageCenterManage/consumer/ConsumerDetail.vue'),
        width: 800,
        bodyStyle: { height: '500px', padding: '10px' },
        centered: true,
        maskClosable: false,
        okButtonProps: {},
        props: {
          row: record
        },
        model: {
        },
        on: {
          async ok (ctl) {
            // const data = ctl.getDialogCtl()
            ctl.closeDialog()
          }
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.consumer-manange {
  background: #fff;
  height: 99.8%;
  padding: 10px;
  .btns {
    height: 40px;
  }
}

</style>
